package com.smart.common.utils;

import com.smart.common.enums.ErrorCode;
import com.smart.common.exception.BusinessException;
import com.smart.common.exception.BusinessNotLoginException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.text.MessageFormat;
import java.util.Collection;

/**
 * 断言工具类
 */
public class AssertUtil {
    public AssertUtil() {
    }

    public static void assertAdminNotNull(Object obj, ErrorCode errorCode) {
        if (obj == null) {
            throw new BusinessNotLoginException(errorCode);
        }
    }

    public static void assertUserNotNull(Object obj, ErrorCode errorCode) {
        if (obj == null) {
            throw new BusinessNotLoginException(errorCode);
        }
    }

    public static void assertEngineerNotNull(Object obj, ErrorCode errorCode) {
        if (obj == null) {
            throw new BusinessNotLoginException(errorCode);
        }
    }

    public static void assertNotNull(Object obj, ErrorCode errorCode) {
        if (obj == null) {
            throw new BusinessException(errorCode);
        }
    }

    public static void assertNotNull(Object obj, String msg) {
        if (obj == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, msg);
        }
    }

    public static void assertNotNull(Object obj, ErrorCode errorCode, String errorMsg) throws BusinessException {
        if (obj == null) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertStrEmpty(String obj, ErrorCode errorCode) {
        if (!StringUtils.isEmpty(obj)) {
            throw new BusinessException(errorCode);
        }
    }

    public static void assertStrNotEmpty(String obj, ErrorCode errorCode) {
        if (StringUtils.isEmpty(obj)) {
            throw new BusinessException(errorCode);
        }
    }

    public static void assertStrNotEmpty(String obj, ErrorCode errorCode, String errorMsg) {
        if (StringUtils.isEmpty(obj)) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertNotEmpty(Collection obj, ErrorCode errorCode, String errorMsg) {
        if (CollectionUtils.isEmpty(obj)) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertNotEmpty(Collection obj, String errorMsg) {
        if (CollectionUtils.isEmpty(obj)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, errorMsg);
        }
    }

    public static void assertEmpty(Collection obj, ErrorCode errorCode, String errorMsg) {
        if (!CollectionUtils.isEmpty(obj)) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertEmpty(Collection obj, String errorMsg) {
        if (!CollectionUtils.isEmpty(obj)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, errorMsg);
        }
    }

    public static void assertNotEmpty(Collection obj, ErrorCode errorCode) {
        assertNotEmpty(obj, errorCode, errorCode.getMsg());
    }

    public static void assertNotEmpty(Object[] objects, ErrorCode errorCode, String errorMsg) {
        if (ArrayUtils.isEmpty(objects)) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertEqual(int int1, int in2, ErrorCode errorCode, String errorMsg, Object... args) {
        if (int1 != in2) {
            String msg = MessageFormat.format(errorMsg, args);
            throw new BusinessException(errorCode, msg);
        }
    }

    public static void assertEqual(int int1, int in2, ErrorCode errorCode) {
        if (int1 != in2) {
            throw new BusinessException(errorCode);
        }
    }

    public static void assertEqual(int int1, int in2, String errorMsg) {
        if (int1 != in2) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, errorMsg);
        }
    }

    public static void assertNotEqual(int int1, int in2, ErrorCode errorCode) {
        if (int1 == in2) {
            throw new BusinessException(errorCode);
        }
    }

    public static void assertNotEqual(int int1, int in2, String errorMsg) {
        if (int1 == in2) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, errorMsg);
        }
    }

    public static void assertNotEqual(int int1, int in2, ErrorCode errorCode, String errorMsg) {
        if (int1 == in2) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertNotEqual(long int1, long in2, ErrorCode errorCode, String errorMsg) {
        if (int1 == in2) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertEqual(long int1, long in2, ErrorCode errorCode, String errorMsg) {
        if (int1 != in2) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertNull(Object obj, String errorMsg) throws BusinessException {
        if (obj != null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, errorMsg);
        }
    }

    public static void assertNull(Object obj, ErrorCode errorCode) throws BusinessException {
        if (obj != null) {
            throw new BusinessException(errorCode);
        }
    }

    public static void assertNull(Object obj, ErrorCode errorCode, String errorMsg) throws BusinessException {
        if (obj != null) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertNotEqual(Integer obj1, Integer obj2, ErrorCode errorCode) throws BusinessException {
        if (obj1 != null && obj2 != null) {
            if (obj1 == obj2) {
                throw new BusinessException(errorCode);
            }
        }
    }

    public static void assertTrue(boolean condition, ErrorCode errorCode) throws BusinessException {
        if (!condition) {
            throw new BusinessException(errorCode);
        }
    }

    public static void assertTrue(boolean condition, String errorMsg) throws BusinessException {
        if (!condition) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, errorMsg);
        }
    }

    public static void assertTrue(boolean condition, ErrorCode errorCode, String errorMsg) throws BusinessException {
        if (!condition) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }

    public static void assertFalse(boolean condition, ErrorCode errorCode) throws BusinessException {
        if (condition) {
            throw new BusinessException(errorCode);
        }
    }

    public static void assertFalse(boolean condition, String errorMsg) throws BusinessException {
        if (condition) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, errorMsg);
        }
    }

    public static void assertFalse(boolean condition, ErrorCode errorCode, String errorMsg) throws BusinessException {
        if (condition) {
            throw new BusinessException(errorCode, errorMsg);
        }
    }
}
